package com.supermarket.controller;

import com.supermarket.common.ResponseResult;
import com.supermarket.entity.Cart;
import com.supermarket.service.CartService;
import com.supermarket.common.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 购物车控制器
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    
    @Autowired
    private RedisUtil redisUtil;

    // 用于接收批量更新选中状态的JSON请求体
    private static class BatchUpdateRequest {
        private Boolean selected;
        
        public Boolean getSelected() {
            return selected;
        }
        
        public void setSelected(Boolean selected) {
            this.selected = selected;
        }
    }

    /**
     * 获取用户购物车列表
     */
    @GetMapping("/list")
    public ResponseResult<List<Cart>> list(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("userId");
        List<Cart> cartList = cartService.getCartListByUserId(userId);
        return ResponseResult.success(cartList);
    }

    /**
     * 添加商品到购物车
     */
    @PostMapping("/add")
    public ResponseResult<Integer> add(@RequestBody Cart cart, HttpServletRequest request) {
        // 如果没设置用户ID，从请求属性中获取
        if (cart.getUserId() == null) {
            Integer userId = (Integer) request.getAttribute("userId");
            cart.setUserId(userId);
        }
        int result = cartService.addCart(cart);
        return ResponseResult.success(result);
    }

    /**
     * 更新购物车商品数量
     */
    @PutMapping("/update/quantity")
    public ResponseResult<Integer> updateQuantity(@RequestParam Integer id, @RequestParam Integer quantity) {
        int result = cartService.updateQuantity(id, quantity);
        return ResponseResult.success(result);
    }

    /**
     * 更新购物车商品选中状态
     */
    @PutMapping("/update/selected")
    public ResponseResult<Integer> updateSelected(@RequestParam Integer id, @RequestParam Boolean selected) {
        int result = cartService.updateSelected(id, selected);
        return ResponseResult.success(result);
    }

    /**
     * 批量更新购物车商品选中状态
     */
    @PutMapping("/update/selected/batch")
    public ResponseResult<Integer> updateSelectedBatch(@RequestParam(required = false) Integer userId, 
                                                      @RequestBody(required = false) BatchUpdateRequest requestBody, 
                                                      HttpServletRequest request) {
        // 如果没有传递用户ID，从请求属性中获取
        if (userId == null) {
            userId = (Integer) request.getAttribute("userId");
        }
        
        // 优先从请求体中获取selected参数
        Boolean selected = (requestBody != null) ? requestBody.getSelected() : null;
        
        // 如果selected为null，则返回错误
        if (selected == null) {
            return ResponseResult.error(400, "缺少selected参数");
        }
        
        int result = cartService.updateSelectedBatch(userId, selected);
        return ResponseResult.success(result);
    }

    /**
     * 删除购物车商品
     */
    @DeleteMapping("/delete")
    public ResponseResult<Integer> delete(@RequestParam Integer id) {
        int result = cartService.deleteCart(id);
        return ResponseResult.success(result);
    }

    /**
     * 批量删除购物车商品
     */
    @DeleteMapping("/delete/batch")
    public ResponseResult<Integer> deleteBatch(@RequestParam List<Integer> ids) {
        int result = cartService.deleteCartBatch(ids);
        return ResponseResult.success(result);
    }

    /**
     * 清空用户购物车
     */
    @DeleteMapping("/clear")
    public ResponseResult<Integer> clear(@RequestParam(required = false) Integer userId, HttpServletRequest request) {
        // 如果没有传递用户ID，从请求属性中获取
        if (userId == null) {
            userId = (Integer) request.getAttribute("userId");
        }
        int result = cartService.clearCart(userId);
        return ResponseResult.success(result);
    }
}
